package com.agrella.chmod;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.ToggleButton;

public class mychmod extends Activity {
    /** Called when the activity is first created. */
	//Setup checkbox objects
	CheckBox cbUserRead, cbUserWrite, cbUserExec;
	CheckBox cbGroupRead, cbGroupWrite, cbGroupExec;
	CheckBox cbOtherRead, cbOtherWrite, cbOtherExec;
	ToggleButton tbSetUID, tbSetGID, tbSetSticky;
	
	//Constants for calculating chmod value
	public static final int CHMODREAD = 4;
	public static final int CHMODWRITE = 2;
	public static final int CHMODEXEC = 1;
	public static final int CHMODSUID = 4;
	public static final int CHMODSGID = 2;
	public static final int CHMODSTICKY = 1;
	
	//Variables to store user, group, other chmod final value
	public int intCHMODUser = 0;
	public int intCHMODGroup = 0;
	public int intCHMODOther = 0;
	public int intCHMODSuper = 0;
	
	//Setup textview object to display CHMOD numeric
	TextView tvCHMOD;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Set the user checkbox objects 
        cbUserRead = (CheckBox) findViewById(R.id.cbUserRead);
        cbUserWrite = (CheckBox) findViewById(R.id.cbUserWrite);
        cbUserExec = (CheckBox) findViewById(R.id.cbUserExec);
        
        //Set the group checkbox objects
        cbGroupRead = (CheckBox) findViewById(R.id.cbGroupRead);
        cbGroupWrite = (CheckBox) findViewById(R.id.cbGroupWrite);
        cbGroupExec = (CheckBox) findViewById(R.id.cbGroupExec);
        
        //Set the other checkbox objects
        cbOtherRead = (CheckBox) findViewById(R.id.cbOtherRead);
        cbOtherWrite = (CheckBox) findViewById(R.id.cbOtherWrite);
        cbOtherExec = (CheckBox) findViewById(R.id.cbOtherExec);
        
        //Set the Set UID,GID,Sticky toggle butons
        tbSetUID = (ToggleButton) findViewById(R.id.tbSetUID);
        tbSetGID = (ToggleButton) findViewById(R.id.tbSetGID);
        tbSetSticky = (ToggleButton) findViewById(R.id.tbSetSticky);
        
        //Configure user checkbox onClick
        cbUserRead.setOnClickListener(mCBURListener);
        cbUserWrite.setOnClickListener(mCBUWListener);
        cbUserExec.setOnClickListener(mCBUEListener);
        //Configure group checkbox onClick
        cbGroupRead.setOnClickListener(mCBGRListener);
        cbGroupWrite.setOnClickListener(mCBGWListener);
        cbGroupExec.setOnClickListener(mCBGEListener);
        //Configure other checkbox onClick
        cbOtherRead.setOnClickListener(mCBORListener);
        cbOtherWrite.setOnClickListener(mCBOWListener);
        cbOtherExec.setOnClickListener(mCBOEListener);
        //Configure set UID,GID,Sticky button onClick
        tbSetUID.setOnClickListener(mTBSUIDListener);
        tbSetGID.setOnClickListener(mTBSGIDListener);
        tbSetSticky.setOnClickListener(mTBStickyListener);
        
        tvCHMOD = (TextView) findViewById(R.id.tvCHMOD);      
    }
    
    //OnClickListeners
    private OnClickListener mCBURListener = new OnClickListener(){
    	public void onClick(View v){
        	if(cbUserRead.isChecked()){
        		intCHMODUser += CHMODREAD;
        		doSetCHMOD();
        	}else if(!cbUserRead.isChecked()){
        		intCHMODUser -= CHMODREAD;
        		doSetCHMOD();
        	};
    	}   	
    };

    private OnClickListener mCBUWListener = new OnClickListener(){
    	public void onClick(View v){
        	if(cbUserWrite.isChecked()){
        		intCHMODUser += CHMODWRITE;
        		doSetCHMOD();
        	}else if(!cbUserWrite.isChecked()){
        		intCHMODUser -= CHMODWRITE;
        		doSetCHMOD();
        	};
    	}   	
    };    
    
    private OnClickListener mCBUEListener = new OnClickListener(){
    	public void onClick(View v){
        	if(cbUserExec.isChecked()){
        		intCHMODUser += CHMODEXEC;
        		doSetCHMOD();
        	}else if(!cbUserExec.isChecked()){
        		intCHMODUser -= CHMODEXEC;
        		doSetCHMOD();
        	};
    	}   	
    }; 
    
    private OnClickListener mCBGRListener = new OnClickListener(){
    	public void onClick(View v){
        	if(cbGroupRead.isChecked()){
        		intCHMODGroup += CHMODREAD;
        		doSetCHMOD();
        	}else if(!cbGroupRead.isChecked()){
        		intCHMODGroup -= CHMODREAD;
        		doSetCHMOD();
        	};
    	}   	
    };

    private OnClickListener mCBGWListener = new OnClickListener(){
    	public void onClick(View v){
        	if(cbGroupWrite.isChecked()){
        		intCHMODGroup += CHMODWRITE;
        		doSetCHMOD();
        	}else if(!cbGroupWrite.isChecked()){
        		intCHMODGroup -= CHMODWRITE;
        		doSetCHMOD();
        	};
    	}   	
    };    
    
    private OnClickListener mCBGEListener = new OnClickListener(){
    	public void onClick(View v){
        	if(cbGroupExec.isChecked()){
        		intCHMODGroup += CHMODEXEC;
        		doSetCHMOD();
        	}else if(!cbGroupExec.isChecked()){
        		intCHMODGroup -= CHMODEXEC;
        		doSetCHMOD();
        	};
    	}   	
    };   
    
    private OnClickListener mCBORListener = new OnClickListener(){
    	public void onClick(View v){
        	if(cbOtherRead.isChecked()){
        		intCHMODOther += CHMODREAD;
        		doSetCHMOD();
        	}else if(!cbOtherRead.isChecked()){
        		intCHMODOther -= CHMODREAD;
        		doSetCHMOD();
        	};
    	}   	
    };

    private OnClickListener mCBOWListener = new OnClickListener(){
    	public void onClick(View v){
        	if(cbOtherWrite.isChecked()){
        		intCHMODOther += CHMODWRITE;
        		doSetCHMOD();
        	}else if(!cbOtherWrite.isChecked()){
        		intCHMODOther -= CHMODWRITE;
        		doSetCHMOD();
        	};
    	}   	
    };    
    
    private OnClickListener mCBOEListener = new OnClickListener(){
    	public void onClick(View v){
        	if(cbOtherExec.isChecked()){
        		intCHMODOther += CHMODEXEC;
        		doSetCHMOD();
        	}else if(!cbOtherExec.isChecked()){
        		intCHMODOther -= CHMODEXEC;
        		doSetCHMOD();
        	};
    	}   	
    };   
    
    private OnClickListener mTBSUIDListener = new OnClickListener(){
    	public void onClick(View v){
    		if(tbSetUID.isChecked()){
    			intCHMODSuper += CHMODSUID;
    			doSetCHMOD();
    		}else if(!tbSetUID.isChecked()){
    			intCHMODSuper -= CHMODSUID;
    			doSetCHMOD();
    		}
    	}
    };
    
    private OnClickListener mTBSGIDListener = new OnClickListener(){
    	public void onClick(View v){
    		if(tbSetGID.isChecked()){
    			intCHMODSuper += CHMODSGID;
    			doSetCHMOD();
    		}else if(!tbSetGID.isChecked()){
    			intCHMODSuper -= CHMODSGID;
    			doSetCHMOD();
    		}
    	}
    }; 
    
    private OnClickListener mTBStickyListener = new OnClickListener(){
    	public void onClick(View v){
    		if(tbSetSticky.isChecked()){
    			intCHMODSuper += CHMODSTICKY;
    			doSetCHMOD();
    		}else if(!tbSetSticky.isChecked()){
    			intCHMODSuper -= CHMODSTICKY;
    			doSetCHMOD();
    		}
    	}
    };    
    
    private void doSetCHMOD(){
    	tvCHMOD.setText("chmod   " + intCHMODSuper + intCHMODUser + intCHMODGroup + intCHMODOther);
    }
}